#include "cell_bookmgr.h"
#include "ui_cell_bookmgr.h"
#include "ui_interface/dlg_bookgetandset.h"

Cell_Bookmgr::Cell_Bookmgr(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Cell_Bookmgr)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);//不能直接更改表格中的元素
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//每次选中会选中一行
    m_model.setHorizontalHeaderLabels(QStringList{"图书id","图书名称","库存","类型1","类型2","价格","图片"});
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//让m_model占满tableView
}

Cell_Bookmgr::~Cell_Bookmgr()
{
    delete ui;
}

void Cell_Bookmgr::initPage(QString strCondtion)
{
    auto l=Sqlmgr::getinstance()->getBook(strCondtion);
    m_model.clear();
    m_model.setHorizontalHeaderLabels(QStringList{"图书id","图书名称","库存","类型1","类型2","价格","图片"});
    for(int i=0;i<l.size();i++)
    {
        QList<QStandardItem*> items;
        for(int j=0;j<l[i].size();j++)
        {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow(items);
    }
}

void Cell_Bookmgr::on_btn_add_clicked()//增加
{
    Dlg_BookADDAndUpdate dlg;
    dlg.exec();
    initPage();
}


void Cell_Bookmgr::on_le_search_textChanged(const QString &arg1)//搜索
{
    QString strCondition=QString("where name like '%%1%' or type1 like '%%1%' or type2 like '%%1%'").arg(arg1);
    initPage(strCondition);
}


void Cell_Bookmgr::on_btn_del_clicked()//删除
{
    int r=ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(nullptr,"信息","无选中图书");
    }
    else
    {
        auto id=m_model.item(r,0)->text();
        auto str=Sqlmgr::getinstance()->delBook(id);
        QMessageBox::information(nullptr,"信息",str.isEmpty()?"删除成功":str);

        initPage();
    }
}


void Cell_Bookmgr::on_bt_updata_clicked()//修改
{
    QStringList BookInfo;
    BookInfo.resize(6);//QStringList是动态数组,在使用下标赋值之前，你可以通过resize函数来设置QStringList的大小，或者在初始化时就设置好固定的大小
    int r=ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(nullptr,"信息","无选中图书");
    }
    else
    {
        BookInfo[0]=m_model.item(r,0)->text();
        BookInfo[1]=m_model.item(r,1)->text();
        BookInfo[2]=m_model.item(r,2)->text();
        BookInfo[3]=m_model.item(r,3)->text();
        BookInfo[4]=m_model.item(r,4)->text();
        BookInfo[5]=m_model.item(r,5)->text();
        Dlg_BookADDAndUpdate dlg;
        auto id=BookInfo[0].toUInt();
        dlg.setType(id,BookInfo);
        dlg.exec();
    }
    initPage();
}





void Cell_Bookmgr::on_btn_borrow_clicked()
{
    int r=ui->tableView->currentIndex().row();
    if(r<0)
    {
        QMessageBox::information(nullptr,"信息","无选中图书");
    }
    else
    {
        auto id=m_model.item(r,0)->text().toUInt();
        QString Bookname=m_model.item(r,1)->text();
        auto cnt=m_model.item(r,2)->text().toUInt();
        if(cnt<=0)
        {
            QMessageBox::information(nullptr,"信息","借阅失败没有库存");
            return;
        }
        Dlg_BookGetAndSet dlg;
        dlg.setBookId(id);
        dlg.setType(true,Bookname);
        int ret=dlg.exec();
        QMessageBox::information(nullptr,"信息",ret?"借阅成功":"借阅失败");
        if(ret)
        {
            initPage();
        }
    }

}


void Cell_Bookmgr::on_btn_search_clicked()
{
    int id=ui->le_search->text().toUInt();
    QString strCondition=QString("where bookid=%1").arg(id);
    initPage(strCondition);
}

